package com.mallcai.bigdata.ladon.controller;

import com.mallcai.bigdata.ladon.config.props.SystemProperties;
import com.mallcai.bigdata.ladon.dpl.entity.system.User;
import com.mallcai.bigdata.ladon.service.system.IAccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-05-20 15:07.
 * Modify:
 */
@Slf4j
@Controller
@RequestMapping("/")
public class HomeController {
    @Autowired
    private SystemProperties sysProps;

    @Autowired
    private IAccountService iAccountService;

    @ResponseBody
    @RequestMapping(value = {"/", "/index"})
    public String home() {
        return "Welcome to cake";
    }

    /**
     * 用户激活url
     *
     * @param emailCode String
     */
    @GetMapping("/activate/{emailCode}")
    public void activeUser(@PathVariable("emailCode") String emailCode, HttpServletResponse resp) {
        User user = null;
        boolean isActive = true;
        String errMsg = "";
        try {
            user = iAccountService.activateUser(emailCode);
        } catch (Exception e) {
            isActive = false;
            errMsg = e.getMessage();
        }
        try {
            resp.setHeader("content-type", "text/html;charset=utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setStatus(200);//设置状态码;
            resp.setHeader("Refresh", "5; URL=" + sysProps.getLoginUri()); // 5秒之后跳转到拉冬登录页面

            String templateName = isActive ? "static/reg-success.html" : "static/reg-err.html";
            InputStream is = ClassLoader.getSystemResourceAsStream(templateName);
            BufferedReader fileReader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                while ((line = fileReader.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                is.close();
                fileReader.close();
            }
            String content = sb.toString();
            if (isActive) {
                content = StringUtils.replace(content, "{0}", user.getUsername()); // 替换用户登录名
            } else {
                content = StringUtils.replace(content, "{0}", errMsg); // 错误信息
            }
            content = StringUtils.replace(content, "{1}", sysProps.getLoginUri()); // 替换登录uri
            resp.getWriter().println(content);
        } catch (Exception e) {
            //
            log.error("用户激活失败~", e);
        }

    }

    @ResponseBody
    @GetMapping("/checkHealth")
    public String checkHealth(){
        return "ok";
    }
}
